package com.rbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbs.common.Constants;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import com.rbs.common.Result;
import java.util.List;

import com.rbs.service.IAdminUserService;
import com.rbs.entity.AdminUser;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lisl
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/admin-user")
public class AdminUserController {

    //1
    @Resource
    private IAdminUserService adminUserService;

    //2
    @PostMapping("/saveAndUpdate")
    public Result saveAndUpdate(@RequestBody AdminUser adminUser){
        if (adminUser.getUsername()!="" || adminUser.getPassword()!="") {
            boolean flag=adminUserService.findSameUsername(adminUser);
            if (flag==false){
                //用户名不存在，保存新的
                return Result.success(adminUserService.saveOrUpdate(adminUser));
            }else {
                //用户名存在
                return Result.error("用户名已经存在");
            }
        }else {
            return Result.error(Constants.CODE_400,"用户名或密码不能为空！");
        }
    }

    //3
    //按用户昵称查询，模糊查询
    @GetMapping("findByUsername")
    public Result findByUserName(@RequestBody AdminUser adminUser){
        List<AdminUser> uList=adminUserService.findByUserName(adminUser);
        return Result.success(uList);
    }

    //按ID查找，模糊查询
    @GetMapping("findByIdm")
    public Result findByIdm(@RequestBody AdminUser adminUser){
        List<AdminUser> iList=adminUserService.findByIdm(adminUser);
        return Result.success(iList);
    }

    //按id单个删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(adminUserService.removeById(id));
    }

    //4
    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(adminUserService.removeBatchByIds(ids));
    }

    //查找全部
    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(adminUserService.list());
    }

    //5
    //按id找单个
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(adminUserService.getById(id));
    }

    //针对id做排序

    /**
     *
     * @param descOrAsc 升序还是降序，true从大到小，false从小到大
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/orderById")
    public Result descByID(@RequestParam boolean descOrAsc,
                            @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){

        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        if (descOrAsc==true){
            queryWrapper.orderByDesc("id");
        }else if (descOrAsc==false){
            queryWrapper.orderByAsc("id");
        }
        return Result.success(adminUserService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

